package com.longnows.appserver.api.v1;


import com.longnows.appserver.dto.TokenDTO;
import com.longnows.appserver.dto.TokenGetDTO;
import com.longnows.appserver.exception.http.NotFoundException;
import com.longnows.appserver.service.WxAuthenticationService;
import com.longnows.appserver.utils.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping(value = "/v1/token")
@RestController
public class TokenController {

    @Autowired
    WxAuthenticationService wxAuthenticationService;

    @PostMapping("")
    public Map<String,String> getToken(@RequestBody @Validated TokenGetDTO userData){
        Map<String,String> map=new HashMap<>();
        String token=null;
        switch(userData.getType()){
            case USER_WX:
                token=  wxAuthenticationService.code2Session(userData.getAccount());
                break;
            case USER_EMAIL:
                break;
            default:
                throw new NotFoundException(10003);

        }
        map.put("token",token);
        return  map;
    }

    @PostMapping ("/verify")
    public Map<String,Boolean> verify(@RequestBody TokenDTO token){
        Map<String,Boolean> map=new HashMap<>();
        Boolean valid = JwtToken.verifyToken(token.getToken());
        map.put("is_valid",valid);
        return  map;
    }

}
